package com.qst.repository;

import com.qst.pojo.entity.Station;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 站点数据访问层
 */
@Repository
public interface StationRepository extends JpaRepository<Station, Integer> {
    
    /**
     * 根据站点名称查找
     */
    Optional<Station> findByName(String name);
    
    /**
     * 检查站点名称是否存在
     */
    boolean existsByName(String name);
    
    /**
     * 根据管理员ID查找站点
     */
    List<Station> findByManagerId(Integer managerId);
    
    /**
     * 根据状态查询
     */
    Page<Station> findByStatus(Station.StationStatus status, Pageable pageable);
    
    /**
     * 根据条件分页查询站点
     */
    @Query("SELECT s FROM Station s WHERE " +
           "(:keyword IS NULL OR s.name LIKE %:keyword% OR s.address LIKE %:keyword%) AND " +
           "(:status IS NULL OR s.status = :status) AND " +
           "(:managerId IS NULL OR s.managerId = :managerId)")
    Page<Station> findByConditions(@Param("keyword") String keyword,
                                    @Param("status") Station.StationStatus status,
                                    @Param("managerId") Integer managerId,
                                    Pageable pageable);
}

